﻿using UnityEditor.Callbacks;
using UnityEditor;
using System.IO;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif

public class XCodeProjectConfig
{

#if UNITY_IOS
    [PostProcessBuild]
    static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            Debug.Log(path);

            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));

            //对所有的编译配置设置选项  
            string target = proj.GetUnityMainTargetGuid();
            OtherSetting(proj, target, projPath);

            //添加依赖库  
            string frameworkTarget = proj.GetUnityFrameworkTargetGuid();
            AddFrameworks(proj, frameworkTarget, projPath);

            //修改plist  
            SetPlist(path);
        }
    }

    //对所有的编译配置设置选项
    static void OtherSetting(PBXProject proj, string target, string projPath)
    {
        proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
        //微信
        proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-Objc");
        proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-all_load");
        //杉德支付将PayFramework.framework放入WeiXinAPI代替WXSDK
        proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Libraries/Scripts/BToolkit/WeiXinAlipayAPI/Plugins/iOS");
        string[] searchPaths = {
            "$(SRCROOT)/Libraries/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/IOSAppControllerOverride/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/CopyAndPaste/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/GetAppInfo/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/GetPhoto/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/GetSystemInfo/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/SavePhotoToAlbum/Plugins/iOS",
            "$(SRCROOT)/Libraries/Scripts/BToolkit/WeiXinAlipayAPI/Plugins/iOS",
        };
        foreach (var item in searchPaths)
        {
            proj.AddBuildProperty(target, "HEADER_SEARCH_PATHS", item);
            proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", item);
        }


        // 保存工程  
        proj.WriteToFile(projPath);
    }

    // 添加依赖库  
    static void AddFrameworks(PBXProject proj, string target, string projPath)
    {
        //微信qq共用
        proj.AddFrameworkToProject(target, "Security.framework", false);
        proj.AddFrameworkToProject(target, "Webkit.framework", false);
        proj.AddFrameworkToProject(target, "SystemConfiguration.framework", false);//支付宝也需要
        proj.AddFrameworkToProject(target, "CoreTelephony.framework", false);
        proj.AddFrameworkToProject(target, "OpenGLES.framework", false);
        proj.AddFrameworkToProject(target, "LocalAuthentication.framework", false);
        proj.AddFrameworkToProject(target, "StoreKit.framework", false);
        proj.AddFrameworkToProject(target, "libz.dylib", false);//支付宝也需要
                                                                //微信专用
        proj.AddFrameworkToProject(target, "libsqlite3.0.dylib", false);
        proj.AddFrameworkToProject(target, "libc++.dylib", false);//支付宝也需要
        proj.AddFrameworkToProject(target, "CFNetwork.framework", false);

        // 保存工程  
        proj.WriteToFile(projPath);
    }

    // 修改plist  
    static void SetPlist(string path)
    {
        string plistPath = path + "/Info.plist";
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));
        PlistElementDict rootDict = plist.root;

        //摄影机权限
        rootDict.SetString("NSCameraUsageDescription", "程序需要使用摄影机");
        //读取相册
        rootDict.SetString("NSPhotoLibraryUsageDescription", "需要从相册读取照片");
        //写入相
        rootDict.SetString("NSPhotoLibraryAddUsageDescription", "需要保存图片到相册");
        //添加白名单
        var array0 = rootDict.CreateArray("LSApplicationQueriesSchemes");
        //微信白名单
        array0.AddString("weixin");
        array0.AddString("wechat");
        array0.AddString("weixinULAPI");
        array0.AddString("alipay");
        // array0.AddString("mqq");
        //插入URL Scheme(只需确保唯一性,可直接用AppId)
        AddURLScheme(plist, new string[][] { new string[] { "weixin", "wx03e68902c4f5f304" } });//URLScheme必须是字母开头	// TODO-微信登录、微信分享

        // 保存plist  
        plist.WriteToFile(plistPath);
    }
    static void AddURLScheme(PlistDocument plist, string[][] urlSchemes)
    {
        var array = plist.root.CreateArray("CFBundleURLTypes");
        for (int i = 0; i < urlSchemes.Length; i++)
        {
            var urlDict = array.AddDict();
            urlDict.SetString("CFBundleTypeRole", "Editor");
            urlDict.SetString("CFBundleURLName", urlSchemes[i][0]);
            var urlInnerArray = urlDict.CreateArray("CFBundleURLSchemes");
            urlInnerArray.AddString(urlSchemes[i][1]);
        }
    }
#endif
}
